/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fmslogger;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author rbs
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        
        File folder = new File(getProgramPath() + "log");
        folder.mkdirs();
        
        File log = new File(getProgramPath() + "log" + File.separator + "fmslauncher.log");
        
        
        SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy - HH:mm:ss");
        String actTime = df.format(new Date());
        if (log.exists())
        {
            BufferedWriter writer = new BufferedWriter(new FileWriter(log, true));
            if (args.length < 3)
                writer.write("<tr bgcolor=\"#AA0000\" align = \"center\"><td>" +  actTime + "</td><td colspan=\"3\">" + "ERROR (nur " + args.length + " Argumente)" + "</tr>");
            else
                writer.write("<tr bgcolor=\"#00AA00\"><td>" +  actTime + "</td><td>" + args[0] + "</td><td>" + args[1] + "</td><td>" + args[2] + "</td></tr>");

            writer.close();
            
        }
        else
        {
            BufferedWriter writer = new BufferedWriter(new FileWriter(log, true));
            writer.write("<html><head><title>FMSLogger</title></head><body text = \"#FFFFFF\" bgcolor = \"#000000\"><table bgcolor = \"#404040\" text = \"#FFFFFF\" border = \"0\" cellspacing = \"5\" cellpadding = \"5\">");
            writer.write("<tr><td>Zeit</td><td>RIC</td><td>FUNC</td><td>TEXT</td>");
            if (args.length < 3)
                writer.write("<tr bgcolor=\"#AA0000\" align = \"center\"><td>" +  actTime + "</td><td colspan=\"3\">" + "ERROR (nur " + args.length + " Argumente)" + "</tr>");
            else
                writer.write("<tr bgcolor=\"#00AA00\"><td>" +  actTime + "</td><td>" + args[0] + "</td><td>" + args[1] + "</td><td>" + args[2] + "</td></tr>");
            writer.close();
        }

        // TODO code application logic here
    }
    
    public static String getProgramPath() {
        
        File file = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().getFile());
        System.out.println("Path: "  +  file.getParent());
        return file.getParent() + File.separator;
    }

}
